// Domien Nowicki
#include "QtBlinkEffect.h"
#include "QtSprite.h"

QtBlinkEffect::QtBlinkEffect(): target(0), blinks(0)
{
}

QtBlinkEffect::~QtBlinkEffect()
{
}

QtBlinkEffect::QtBlinkEffect(const QtBlinkEffect& rhs): QtSingleAnimationEffect(rhs), target(rhs.target), blinks(rhs.blinks), blinkDuration(rhs.blinkDuration), alpha(rhs.alpha)
{

}

void QtBlinkEffect::SetTarget(QtSprite *sprite)
{
	target = sprite;
}

QtSprite* QtBlinkEffect::Target() const
{
	return target;
}

void QtBlinkEffect::Update(qreal value)
{
	if (target && blinks)
	{	
		qreal blinkTime = value * blinks;
		int a = (alpha + blinkTime)/2;
		qreal t = (alpha+blinkTime) - a*2;

		if (t > 1)
			value = 2 - t;
		else
			value = t;

		target->SetAlpha( value );
	}
}

void QtBlinkEffect::SetBlinkDuration(unsigned int newDuration)
{
	blinkDuration = newDuration;
}

void QtBlinkEffect::SetupAnimation()
{
	if (target && blinkDuration)
	{
		blinks = Duration() / blinkDuration;
		if (blinks&1)
			blinks++;

		alpha = target->Alpha();
	}
}

QtAnimationEffect* QtBlinkEffect::Clone() const
{
	return new QtBlinkEffect(*this);
}

